{include $data.pathToError}
<script type="text/javascript">
    {literal}
        
    function changeOriginalAmount(tariff, amount) {
        //ramount_original
        if (tariff==6){
            $('#ramount_original_edit').val("0");
        } else {
            $('#ramount_original_edit').val(amount);
        }
    }
        
    function setPostsToDescription() {
        var alls = $('.post_to_desc:checkbox:checked');
        var desc = '';
        alls.each(function(){
            var id = $(this).val();
            var text = $("#post_to_desc_"+id).val();
            desc += text + '; ';
        });    
        if (desc != '')
            desc = ' - ' + desc;
        var title = $("#title").val();
        title += desc;
        
        var regex = /(<([^>]+)>)/ig;
        var title = title.replace(regex, "");
        
        if ($("#rtariff").val() != 32){
            $("#rdescription").val('');
            $("#rdescription").val(title);
        }
    }
        
    function showRecordForm(){
        var display = $(".inlineFormTr").css("display");
        if (display == 'none') {
            setPostsToDescription();
            unexpended($('#rdate').val(), $('#rclient').val());
            if ($.browser.msie)
                $(".inlineFormTr").css("display", "block");
            else
                $(".inlineFormTr").css("display", "table-row");
            $("#rdate").focus();       
        } else {
            $(".inlineFormTr").css("display", "none");
            $("#title").focus();           
        }
    }
        
    function sendAction(action){
        $("#action").val(action);
        $("#editing").submit();    
    }    
        
    function showOther(){
        var display = $("#otherTable").css("display");
        if (display == "none"){
            $("#other").empty();
            $("#other").append("- Task features");
            $("#otherTable").show();
            $("#showOther").val("1");
        } else {
            $("#other").empty();
            $("#other").append("+ Task features");
            $("#otherTable").hide();
            $("#showOther").val("0");
        }
    }
    
    function showTaskTypeTable(id){
        $("#taskType").removeClass("bgSpecial1");
        $(".external").hide();    
        if (id==8 || id==9){
            $("#taskType").addClass("bgSpecial1");
            $("#taskType"+id).show();
        }
    }
    
    function change_responsible(id){
        if (id!=""){
            $("#responsible option[value="+id+"]").attr("selected", "selected");
            $("#worker option[value="+id+"]").attr("selected", "selected");
        } else {
            $("#responsible option[value=-1]").attr("selected", "selected");
            $("#worker option[value=-1]").attr("selected", "selected");
        }
    }
        
    function change_worker(id){
        $("#worker option").removeAttr("selected");
        $("#worker option[value="+id+"]").attr("selected", "selected");
    }
        
    function changeTariffs(value, target){
        if (typeof(target) == 'undefined')
            target = 'rtariff';
        
        if (value!=-1){
            
            var data = {
                action : 'change_tariffs',
                params: {
                    type: value
                }
            }

            var result = false;

            ajax(data, function (data) {
                if (data.result==0){
                    $("#"+target).empty();
                    $("#"+target).append('<option value="-1">Please select</option>');
                    result = false;
                } else {
                    $("#"+target).empty();
                    $("#"+target).append('<option value="-1">Please select</option>');
                    $("#"+target).append(data.options)
                    result = true;
                }
            });

            return result;
            
        }
    }
        
    function fillPrice(value, target){
        if (typeof(target) == 'undefined')
            target = 'rprice';
        if (value!=-1){
            
            var data = {
                action : 'fill_price',
                params: {
                    tariff: value
                }
            }

            var result = false;

            ajax(data, function (data) {
                if (data.result==0){
                    $("#"+target).val("0.00");
                    result = false;
                } else {
                    $("#"+target).val(data.price);
                    makeFloat(".double",2);
                    result = true;
                }
            });

            return result;
            
        }
    }
        
    $(document).ready(function() {
        $("#task_start").datepicker({ 
            dateFormat: "dd/mm/yy",
            firstDay: 1      
        });
        $("#deadline").datepicker({ 
            dateFormat: "dd/mm/yy" ,
            firstDay: 1     
        });
        $("#date_of_action").datepicker({ 
            dateFormat: "dd/mm/yy" ,
            firstDay: 1     
        }); 
        $("#delivery").datepicker({ 
            dateFormat: "dd/mm/yy" ,
            firstDay: 1     
        }); 
        $(".float").change(function() {
            makeFloat(".float",1);
        });
        $("#rdate").datepicker({ 
            dateFormat: "dd/mm/yy" ,
            firstDay: 1     
        });
        $("#rdate_edit").datepicker({ 
            dateFormat: "dd/mm/yy" ,
            firstDay: 1     
        });
        $(".double").change(function() {
            makeFloat(".double",2);
        }) 
    {/literal}
    {if $data.isCopy == 1}
        showRecordForm()
    {/if}
    {if isset($smarty.request.editRecord)}
        unexpended($('#rdate_edit').val(), $('#rclient_edit').val());
    {/if}
    {literal}
        showTaskTypeTable({/literal}{$data.task_data.type}{literal});
    });
    {/literal}
</script>

<h2 class="pageTitle">Edit task</h2>

<form class="adding" id="editing" action="{$data.root}?input=edit_task&task={$smarty.request.task}" method="post" onsubmit="return checkAdd_task(
                                                                    $('#title').val(), 
                                                                    $('#client').val(),
                                                                    $('#responsible').val(),
                                                                    $('#deadline').val(), 
                                                                    $('#type').val(), 
                                                                    $('input:radio[name=task_data[status]]:checked').val(), 
                                                                    $('input:radio[name=task_data[priority]]:checked').val(), 
                                                                    'prazdno',
                                                                    $('#duration').val())">
    <table class="tableForm">
        <tr>
            <td>
                <label for="title">Title <span class="red">*</span> </label>
            </td>
            <td colspan="2">
                <input name="task_data[title]" id="title" type="text" value="{$data.task_data.title}" size="100" />
            </td>
        </tr>
        <tr>
            <td>
                <label for="client">Client <span class="red">*</span> </label>
            </td>
            <td>
                <select name="task_data[client]" id="client" size="1" onchange="change_responsible($('#client option:selected').attr('rel'))">
                    <option value="-1">Please select</option>
                    {foreach $data.clients as $key => $client}
                        <option {if $smarty.session.user.user_type_id!=$smarty.const.USER_TYPE_CLIENT && $client.bad_payer==1}class="badPayer"{/if} rel="{$client.contactPerson}" {if $client.klient_id==$data.task_data.client}selected="selected"{/if} value="{$client.klient_id}">{$client.name}</option>
                    {/foreach}
                </select>
            </td>
            <td rowspan="5" style="width: 760px">
                Description:
                {include "include/phorum.tpl"}
                New post:<br/><textarea cols="70" rows="5" name="phorum[text]"></textarea>
            </td>
        </tr>
        <tr>
            <td>
                <label for="responsible">Responsible <span class="red">*</span> </label>
            </td>
            <td>
                <select name="task_data[responsible]" id="responsible" size="1" onchange="change_worker(this.value)">
                    <option value="-1">Please select</option>
                    {foreach $data.responsibles as $key => $responsible}
                        <option value="{$responsible.users_id}" {if $responsible.users_id==$data.task_data.responsible}selected="selected"{/if}>{$responsible.surname}, {$responsible.name}</option>
                    {/foreach}
                </select>
            </td>
        </tr>
        <tr>
            <td>
                <label for="worker">Worker</label>
            </td>
            <td>
                <select name="task_data[worker]" id="worker" size="1">
                    <option value="-1">Please select</option>
                    {foreach $data.responsibles as $key => $responsible}
                        <option value="{$responsible.users_id}" {if $responsible.users_id==$data.task_data.worker}selected="selected"{/if}>{$responsible.surname}, {$responsible.name}</option>
                    {/foreach}
                </select>
            </td>
        </tr>
        <tr>
            <td>
                <label for="deadline">Deadline <span class="red">*</span> </label>
            </td>
            <td>
                <input name="task_data[deadline]" id="deadline" type="text" style="width: 70px" value="{$data.task_data.deadline}" />
            </td>
        </tr>
        <tr>
            <td><label for="duration">Expected amount <span class="red">*</span> </label></td>
            <td>
                <input class="float" name="task_data[duration]" id="duration" style="width: 30px" type="text" size="5" maxlength="5" value="{$data.task_data.duration}" />&nbsp;&nbsp;{if $data.task_data.rate!=-1}{$data.task_data.rate}{else}-{/if}%
            </td>
        </tr>
        <tr>
            <td>
                <label for="priority">Priority <span class="red">*</span> </label>
            </td>
            <td><b style="font-size: 16px">&DoubleUpArrow;</b>
                {foreach $data.priorities as $key => $priority}
                    <input {if $priority.id==$data.task_data.priority}checked="checked"{/if}name="task_data[priority]" type="radio" value="{$priority.id}" />
                {/foreach}<b style="font-size: 16px">&DoubleDownArrow;</b>
            </td>
        </tr>

        <tr>
            <td></td>
            <td></td>
            <td></td>
        </tr>
        <tr>
            <td colspan="3"><a style="color: black; text-decoration: none;" href="javascript: showOther()"><b id="other">+ Task features</b></a></td>
        </tr>
        <tr id="otherTable" class="bgGray">
            <td colspan="3">
                <table style="width: auto">
                    <tr>
                        <td>
                            <label for="status">Status <span class="red">*</span> </label>
                        </td>
                        <td>
                            <input name="oldStatus" type="hidden" value="{$data.task_data.status}" />
                            {foreach $data.statuses as $key => $status}
                                <input {if $status.id_stav==$data.task_data.status}checked="checked"{/if}name="task_data[status]" type="radio" value="{$status.id_stav}" />&nbsp;{$status.stav}<br/>
                            {/foreach}
                        </td>
                        <td></td>
                    </tr>
                    <tr id="taskType">
                        <td>
                            <label for="type">Task type <span class="red">*</span> </label>
                        </td>
                        <td>
                            <select name="task_data[type]" id="type" size="1" onchange="showTaskTypeTable(this.value)">
                                {foreach $data.types as $key => $type}
                                    <option {if $type.Id==$data.task_data.type}selected="selected"{/if} value="{$type.Id}">{$type.type_of_record}</option>
                                {/foreach}
                            </select>
                        </td>
                        <td rowspan="5" id="external">
                            <table class="external bgSpecial1" id="taskType8">
                                <tr>
                                    <td style="width: 20em">Expected date of delivery</td>
                                    <td><input name="goods[delivery]" type="text" id="delivery" value="{$data.task_data.order_of_goods.delivery}" /></td>
                                </tr>
                                <tr>
                                    <td style="width: auto">Real delivery time</td>
                                    <td><input name="goods[real]" type="text" value="{$data.task_data.order_of_goods.real}" /></td>
                                </tr>
                                <tr>
                                    <td>Type of goods</td>
                                    <td>
                                        <select name="goods[type_of_good]" size="1">
                                            {foreach $data.goods_types as $key => $goods}
                                                <option value="{$goods.id}" {if $goods.id == $data.task_data.order_of_goods.type_of_good}selected="selected"{/if}>{$goods.type}</option>
                                            {/foreach}
                                        </select> 
                                    </td>
                                </tr>
                            </table>
                            <table class="external bgSpecial1" id="taskType9">
                                <tr>
                                    <td>Operation</td>
                                    <td>
                                        <select name="operation[operation]" size="1">
                                            <option value="">Please select</option>
                                            {foreach $data.operation_types as $key => $operation}
                                                <option {if $operation.id == $data.task_data.operation_with_user.operation}selected="selected"{/if}value="{$operation.id}">{$operation.operation}</option>
                                            {/foreach}
                                        </select> 
                                    </td>
                                    <td>Date of operation</td>
                                    <td><input name="operation[date_of_action]" type="text" id="date_of_action" value="{$data.task_data.operation_with_user.date_of_action}" /></td>
                                </tr>
                                <tr>
                                    <td>Name</td>
                                    <td><input name="operation[name]" type="text" value="{$data.task_data.operation_with_user.name}" /></td>
                                    <td>Surname</td>
                                    <td><input name="operation[surname]" type="text" value="{$data.task_data.operation_with_user.surname}" /></td>
                                </tr>
                                <tr>
                                    <td>Title</td>
                                    <td><input name="operation[title]" type="text" value="{$data.task_data.operation_with_user.title}" /></td>
                                    <td>External address</td>
                                    <td><input name="operation[address]" type="text" value="{$data.task_data.operation_with_user.address}" /></td>
                                </tr>
                                <tr>
                                    <td>Phone Number</td>
                                    <td><input name="operation[phone]" type="text" value="{$data.task_data.operation_with_user.phone}" /></td>
                                    <td>Email</td>
                                    <td><input name="operation[email]" type="text" value="{$data.task_data.operation_with_user.email}" /></td>
                                </tr>
                                <tr>
                                    <td>Position</td>
                                    <td><input name="operation[position]" type="text"  value="{$data.task_data.operation_with_user.position}"/></td>
                                    <td>Department</td>
                                    <td><input name="operation[department]" type="text"  value="{$data.task_data.operation_with_user.department}"/></td>
                                </tr>
                            </table>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <label for="task_start">Task start <span class="red"></span> </label>
                        </td>
                        <td>
                            <input name="task_data[task_start]" id="task_start" type="text" value="{$data.task_data.task_start}" />
                        </td>
                        <td></td>
                    </tr>
                    <tr>
                        <td></td>
                        <td></td>
                    </tr>
                    <tr>
                        <td><label for="problem_type">Type of problem</label></td>
                        <td>
                            <select name="task_data[problem_type]" id="problem_type" size="1" >
                                <option value="">Please select</option>
                                {foreach $data.problem_types as $key => $ptype}
                                    <option {if $data.task_data.problem_type==$ptype.id}selected="selected"{/if} value="{$ptype.id}">{$ptype.problem}</option>
                                {/foreach}
                            </select>
                        </td>
                    </tr>
                    <tr>
                        <td><label for="consult">Consult at a meeting</label></td>
                        <td>
                            <input {if $data.task_data.consult==1}checked="checked"{/if} name="task_data[consult]" id="consult" type="checkbox" value="1" />
                            <input name="showOther" id="showOther" type="hidden" value="0" />
                        </td>
                    </tr>
                    <tr>
                        <td colspan="3" style="padding: 0; padding-bottom: 5px">
                            {if $smarty.session.user.user_id==$data.task_data.creator}
                                <a style="line-height: 25px; color: red" href="javascript: deleteTask({$data.task_data.taskId})"><img src="{$data.root}images/delete.png" title="" alt="" height="25" width="25" style="float: left; margin-right: 3px" /> DELETE</a>
                                {/if}
                                {if $smarty.session.user.user_type_id > $smarty.const.USER_TYPE_CLIENT}
                                <a target="_blank" class="baseLink" style="line-height: 25px;" href="{$data.root}?input=receipt_delivery&taskId={$data.task_data.taskId}">Print receipt / delivery report</a> 
                            {/if}
                        </td>
                    </tr>
                </table>
            </td>
        </tr>
        <tr class="submitRow">
            <td>
                <input name="action" value="addTask" type="hidden" id="action" />
                <span class="red">* Required</span>
            </td>
            <td colspan="2">
                <input type="button" value="Save" name="add_task" onclick="sendAction('addTask')" /> &nbsp; 
                {*<input type="button" value="Write & Complete" name="add_task" onclick="sendAction('addTask_complete')" /> &nbsp; 
                <input type="button" value="Write & TimesheetRecord" name="add_task" onclick="sendAction('addTask_record')" /> &nbsp; 
                <input type="button" value="Write & TimesheetRecord & Complete" name="add_task" onclick="sendAction('addTask_record_complete')" /> &nbsp; 
                <br/>
                *}<input type="button" value="Save & Send" name="add_task" onclick="sendAction('addTask_send')" /> &nbsp; 
                <input type="button" value="Save & Complete & Send" name="add_task" onclick="sendAction('addTask_complete_send')" /> &nbsp; 
                {*<input type="button" value="Write & TimesheetRecord & Send" name="add_task" onclick="sendAction('addTask_record_send')" /> &nbsp; 
                <input type="button" value="Write & TimesheetRecord & Complete & Send" name="add_task" onclick="sendAction('addTask_record_complete_send')" /> &nbsp; *}
            </td>
        </tr>
    </table>
</form>

{if $smarty.session.user.user_type_id != $smarty.const.USER_TYPE_CLIENT}
    <div class="menu-list">
        <a href="javascript: showRecordForm()" class="basicButton">Add new record</a> *required
    </div>
    <form class="adding" action="{$data.root}?input=edit_task&task={$smarty.request.task}" method="post" onsubmit="return checkAdd_record(
                                                                    $('#rdate').val(), 
                                                                    $('#rclient').val(),
                                                                    $('#rtype').val(),
                                                                    $('#ramount').val(),
                                                                    $('#rprice').val(),
                                                                    $('#rdescription').val(),
                                                                    $('#ramount_original').val(),
                                                                    $('#rtariff').val()
                                                                                   )">
        <table class="inlineForm">
            <tr>
                <th style="width: 100px" ><label for="rdate">Date *</label></th>
                <th style="width: 120px" ><label for="rclient">Client *</label></th>
                <th style="width: 150px" ><label for="rtype">Type of record *</label></th>
                <th style="width: 80px"><label for="ramount">Amount *</label></th>
                <th style="width: 120px"><label for="rtariff">Tariff</label></th>
                <th style="width: 200px" ><label for="rdescription">Description *</label></th>
                <th style="width: 80px"><label for="rprice">Unit price *</label></th>
                <th style="width: 120px"><label for="ruser_id">Added by</label></th>
                <th style="width: 50px"></th>
                <th style="width: 50px"></th>
            </tr>
            {if $smarty.session.user.user_type_id != $smarty.const.USER_TYPE_CLIENT}
                <tr class="inlineFormTr">
                    <td style="width: 100px" >
                        <input style="width: 100px" name="record[date]" id="rdate" type="text" value="{$data.prefill.date}" onchange="unexpended($('#rdate').val(), $('#rclient').val());" />
                    </td>
                    <td style="width: 120px" >
                        <select style="width: 120px" name="record[client]" id="rclient" size="1" onchange="unexpended($('#rdate').val(), $('#rclient').val());" >
                            <option value="-1">Please select</option>
                            {foreach $data.clients as $key => $client}
                                <option {if $smarty.session.user.user_type_id!=$smarty.const.USER_TYPE_CLIENT && $client.bad_payer==1}class="badPayer"{/if} {if $data.prefill.client==$client.klient_id}selected="selected"{/if} value="{$client.klient_id}">{$client.name}</option>
                            {/foreach}
                        </select>
                    </td>
                    <td style="width: 150px" >
                        <select style="width: 150px" name="record[type]" id="rtype" size="1" onchange="changeTariffs(this.value)">
                            <option value="-1">Please select</option>
                            {foreach $data.rtypes as $key => $type}
                                <option {if $type.Id==$data.prefill.type}selected="selected"{/if} value="{$type.Id}">{$type.type_of_record}</option>
                            {/foreach}
                        </select>
                    </td>
                    <td style="width: 80px" >
                        <input style="width: 80px" name="record[amount]" id="ramount" class="float" type="text" value="{$data.prefill.amount}"  />
                        <input id="ramount_original" type="hidden" value="0"  />
                    </td>
                    <td style="width: 120px" >
                        <select style="width: 120px" name="record[tariff]" id="rtariff" size="1" onchange="fillPrice(this.value); fillDescription('#rdescription', this.value)">
                            <option value="-1">Please select</option>
                            {foreach $data.rtariffs as $key => $tariff}
                                <option {if $tariff.Id==$data.prefill.tariff}selected="selected"{/if} value="{$tariff.Id}">{$tariff.title}</option>
                            {/foreach}
                        </select>
                    </td>
                    <td style="width: 200px" >
                        <input style="width: 200px" name="record[description]"  maxlength="512" id="rdescription" type="text" value="{if $data.prefill.description}{$data.prefill.description}{/if}"/>
                    </td>
                    <td style="width: 80px" >
                        <input style="width: 80px" name="record[price]" id="rprice" class="double" type="text" value="{$data.prefill.unit_price}" />
                    </td>
                    <td style="width: 120px" >
                        <select style="width: 120px" name="record[user_id]" id="ruser_id" size="1" {*onchange="showReport(this.value)"*}>
                            {foreach $data.rusers as $key => $user}
                                <option {if $data.prefill.user_id==$user.users_id}selected="selected"{/if} value="{$user.users_id}">{$user.name} {$user.surname}</option>
                            {/foreach}
                        </select>
                    </td>
                    <td style="width: 50px" >
                        <input name="action" value="add_record" type="hidden" />
                        <input type="submit" value="Save" name="add_record" />
                    </td>
                    <td style="width: 50px" ></td>
                </tr> 
            {/if}
        </table>
    </form>
{/if}

<form action="{$data.root}?input=edit_task&task={$smarty.request.task}" method="post" onsubmit="return checkAdd_record(
                                                                    $('#rdate_edit').val(), 
                                                                    $('#rclient_edit').val(),
                                                                    $('#rtype_edit').val(),
                                                                    $('#ramount_edit').val(),
                                                                    $('#rprice_edit').val(),
                                                                    $('#rdescription_edit').val(),
                                                                    $('#ramount_original_edit').val(),
                                                                    $('#rtariff_edit').val()
                                                                                   )">
    <table class="inlineForm">
        <tr>
            <td style="width: 100px" ></td>
            <td style="width: 120px"></td>
            <td style="width: 150px" ></td>
            <td style="width: 80px"></td>
            <td style="width: 120px" ></td>
            <td style="width: 200px" ></td>
            <td style="width: 80px"></td>
            <td style="width: 120px" ></td>
            <td style="width: 50px"></td>
            <td style="width: 50px"></td>
        </tr>

        {assign var=counter value=1}
        {if $data.task_data.records[0]}
            {foreach from=$data.task_data.records key="key" item="record"}
                {if $smarty.session.user.user_type_id != $smarty.const.USER_TYPE_CLIENT}
                    {assign var=rights value='full'}
                {else}
                    {assign var=rights value='nofull'}
                {/if}
                {include file='record_item.tpl' type=$rights record=$record counter=$counter}
                {if $counter==1}
                    {assign var=counter value=2}
                {else}
                    {assign var=counter value=1}
                {/if}
            {/foreach}
        {else}    
            <td style="text-align: center; padding: 15px 0; color: #969696" colspan="11">Assigned records do not exist for this task.</td>
        {/if}
    </table>
</form>
